package com.deke.teacher.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.teacher.dao.EduTeacherVoDao;
import com.deke.teacher.service.EduTeacherVoService;
import com.deke.entity.EduTeacherVo;
import com.deke.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：张恒（1371745897@qq.com）
 * @date ：Created in 2022/12/7 12:32
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class EduTeacherVoServiceImpl implements EduTeacherVoService {
    @Resource
    private EduTeacherVoDao eduTeacherVoDao;

    /**
     * 分页查询所有
     *
     * @param current      当前页码
     * @param pageSize     每页几条数据
     * @param eduTeacherVo 实体类
     * @return
     */
    @Override
    public Result<IPage<EduTeacherVo>> queryByConditionPage(EduTeacherVo eduTeacherVo, Integer current, Integer pageSize) {
        IPage<EduTeacherVo> page = new Page<>(current, pageSize);
        QueryWrapper<EduTeacherVo> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(eduTeacherVo.getName())) {
            wrapper.like("name", eduTeacherVo.getName());
        }
        if (StringUtils.hasText(eduTeacherVo.getMobile())) {
            wrapper.like("mobile", eduTeacherVo.getMobile());
        }
        if (StringUtils.hasText(eduTeacherVo.getStatus())) {
            wrapper.eq("status", eduTeacherVo.getStatus());
        }
        IPage<EduTeacherVo> iPage = eduTeacherVoDao.selectPage(page, wrapper);
        return new Result(2000, "查询讲师信息成功", iPage);
    }

    /**
     * 修改讲师状态
     *
     * @param id  讲师id
     * @param status  讲师状态
     * @return
     */
    @Override
    public Result updateStatus(String id, String status) {
        EduTeacherVo eduTeacherVo = new EduTeacherVo();
        eduTeacherVo.setId(id);
        eduTeacherVo.setStatus(status);
        int i = eduTeacherVoDao.updateById(eduTeacherVo);
        if (i > 0) {
            return new Result(2000, "修改成功");
        }
        return new Result(5000, "修改失败");
    }

    /**
     * 修改
     * @param eduTeacherVo 讲师信息类
     * @return
     */
    @Override
    public Result update(EduTeacherVo eduTeacherVo) {
        int i = eduTeacherVoDao.updateById(eduTeacherVo);
        if (i>0){
            return new Result(2000,"修改成功");
        }
        return new Result(5000,"修改失败");
    }


    /**
     * 添加
     * @param eduTeacherVo 讲师信息实体类
     * @return
     */
    @Override
    public Result qianaddTeacher(EduTeacherVo eduTeacherVo){
        //get算法的实例(获取100000到199999中间的任意数)
        SecureRandom secureRandom= null;
        try {
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        long l = secureRandom.nextInt(100001) + 99999;
        //将数字转化为字符串
        String s = String.valueOf(l);
        //获取当前时间戳
        long time=new Date().getTime();
        String ss=""+time;
        //两个字符串相加
        String id=s+ss;
        eduTeacherVo.setId(id);
        //获取当前时间
        Date date = new Date();
        eduTeacherVo.setGmtCreate(date);
        eduTeacherVo.setGmtModified(date);

        int insert = eduTeacherVoDao.insert(eduTeacherVo);
        if (insert>0){
            return new Result(2000,"添加成功",insert);
        }
        return new Result(5000,"添加失败");
    }

    /**
     * 根据讲师名查询讲师信息
     * @param name 实体类
     * @return
     */
    @Override
    public Result queryAllTeacherByTeacherName(String name) {
        EduTeacherVo eduTeacherVo = eduTeacherVoDao.queryAllTeacherByTeacherName(name);
        if (ObjectUtil.isNotNull(eduTeacherVo)){
            return new Result(2000,"查询成功",eduTeacherVo);
        }
        return new Result(5000,"查询失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        EduTeacherVo eduTeacherVo = eduTeacherVoDao.selectById(id);
        if (eduTeacherVo.getIsDeleted()!="0"){
            int i = eduTeacherVoDao.deleteById(id);
            if (i>0){
                return new Result(2000,"删除成功");
            }
        }
        return new Result(5000,"删除失败");
    }
}